package com.thinkTank.oa.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.thinkTank.oa.entity.Permission;
import com.thinkTank.oa.entity.Role;
import com.thinkTank.oa.entity.User;
import com.thinkTank.oa.service.PermissionService;
import com.thinkTank.oa.service.RoleService;

/***
 * 角色类
 * @date 2018-08-09
 * @author dq
 *
 */
@RequestMapping("/role")
@Controller
public class RoleController extends BaseController {
	
	private final Logger logger = LoggerFactory.getLogger(RoleController.class);
	
	@Autowired
	private RoleService roleService;
	@Autowired
	private PermissionService permissionService;
	/**
	 * 显示角色列表
	 * @return
	 */
	@RequestMapping("/showRoleList")
	public String showRoleList(Integer pageNo,HttpServletRequest request,String searchKeyword){
		try{
			if(null==pageNo){
				pageNo=1;
			}
			 PageHelper.startPage(pageNo, DEFAULT_COUNT);
			 Role role = new Role();
			 if(searchKeyword!=null&&"".equals(searchKeyword)){
				 role.setName(searchKeyword);
			 }
			 List<Role> list = roleService.queryAll(role);
			 PageInfo<Role> pageInfo = new PageInfo<Role>(list);
			 request.setAttribute("pageInfo", pageInfo);
			 request.setAttribute("searchKeyword", searchKeyword);
		}catch(Exception e){
			logger.equals(e.getMessage());
		}
		return "showrolelist";
	}
	/**
	 * 显示添加角色页面
	 * @author dq
	 * @date 2018-08-16
	 * @return
	 */
	@RequestMapping("/showAddRole")
	public String showAddRole(Model model){
		try{
			model.addAttribute("role", new Role());
			/**
			 * 查询所有的菜单 
			 */
			 List<Permission> menuList = permissionService.queryAllMenu();
			 //System.out.println(menuList.size());
			 model.addAttribute("menuList", menuList);
			/**
			 * 查询所有的功能
			 */
			 List<Permission> permissionList =permissionService. queryAllPermission();
			 model.addAttribute("permissionList", permissionList);
			 
		}catch(Exception e){
			logger.equals(e.getMessage());
		}
		return "showaddrole";
	}
	/**
	 * 显示添加角色
	 * @author dq
	 * @date 2018-08-16
	 * @param menu 选中的菜单集合，permissi选中的功能列表
	 * @return
	 */
	@RequestMapping("/addRole")
	public  String addRole(@Valid Role role,BindingResult result,Model model,String menu,String permissi){
		try{
			if (result.hasErrors()) {
				model.addAttribute("role", role);
				return showAddRole(model);
			}
			roleService.addRoleAndPermission(role, menu, permissi);
		}catch(Exception e){
			logger.equals(e.getMessage());
		}

		
		return "redirect:/role/showRoleList";
	}
	@RequestMapping("/showUpdateRole")
	public String showUpdateRole(Integer roleId,Model model){
		Role role = roleService.getById(roleId);
		if(!model.containsAttribute("role")){
			model.addAttribute("role", role);
		}
		/**
		 * 查询所有的菜单 
		 */
		 List<Permission> menuList = permissionService.queryAllMenu();
		 //System.out.println(menuList.size());
		 model.addAttribute("menuList", menuList);
		/**
		 * 查询所有的功能
		 */
		 List<Permission> permissionList =permissionService. queryAllPermission();
		 model.addAttribute("permissionList", permissionList);
		 //用户关联的菜单和功能
		 /**
		  * 查询用户关联的菜单
		  * 查询用户关联的功能
		  */
		List<Permission> permissionUserList  = permissionService.findPermissionByRoleId(roleId);
		List<String> menuUserList = new ArrayList<String>();
		List<String> menuPerList = new ArrayList<String>();
		for(Permission per :permissionUserList){
			if(per.getParentId()!=null&&per.getParentId()>=0){
				menuUserList.add(per.getUrl());
			}else{
				if(per.getParentId()==null){
					menuPerList.add(per.getUrl());
				}
			}
		}
		
		
		model.addAttribute("menuUserList", menuUserList);
		 model.addAttribute("menuPerList", menuPerList);
		return "showupdaterole";
	}
	@RequestMapping("/updateRole")
	public String updateRole(@Valid Role role,BindingResult result,Model model,String menu,String permissi){
		if (result.hasErrors()) {
			model.addAttribute("role", role);
			return showUpdateRole(role.getPid(),model);
		}
		roleService.updateRoleAndPerMission(role, menu, permissi);
		
		return "redirect:/role/showRoleList";
		
	}
}
